<template>
  <div>
    <el-form
      class="app-container"
      ref="elForm"
      :model="logList"
      size="medium"
      :inline="true"
      label-width="100px"
    >
      <!-- <el-form-item label="发送人:" prop="type">
        <el-select
          v-model="logList.type"
          placeholder="请选择"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in field101Options"
            :key="index"
            :label="item.label"
            :value="item.label"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="发送状态:" prop="issuccess">
        <el-select
          v-model="logList.issuccess"
          placeholder="请选择"
          @change="getStatus"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in field101Options"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发送日期:" prop="lastsendtime">
        <el-date-picker
          v-model="logList.lastsendtime"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :style="{ width: '100%' }"
          placeholder="请选择提交时间"
          clearable
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="搜索:" prop="smscontent">
        <el-input
          v-model="logList.smscontent"
          placeholder="请输入短信内容搜索"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label prop="field107">
        <el-button
          type="primary"
          size="medium"
          v-hasPermi="['marketing/noteList/query']"
          @click="search"
          >搜索</el-button
        >
      </el-form-item>
      <el-button
        type="primary"
        size="medium"
        v-hasPermi="['marketing/noteList/edit']"
        @click="getSend"
        >发送短信</el-button
      >
    </el-form>
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 95%; margin: auto"
      :header-cell-style="{ background: '#f7f7f7' }"
    >
      <el-table-column
        type="index"
        label="序号"
        align="center"
      ></el-table-column>
      <el-table-column prop="phonenumbers" label="接收人" align="center">
        <template slot-scope="scope">
          <span slot="reference" v-if="scope.row.phonenumbers.length < 18">{{
            scope.row.phonenumbers
          }}</span>
          <span slot="reference" v-if="scope.row.phonenumbers.length > 18">{{
            scope.row.phonenumbers.substring(0, 18) + "..."
          }}</span>
          <a
            @click="getAll(scope.row)"
            v-if="scope.row.phonenumbers.length > 18"
            style="color:blue"
            >查看全部</a
          >
        </template>
      </el-table-column>
      <el-table-column prop="registertime" label="发送时间" align="center">
        <template slot-scope="scope">{{
          newDate(scope.row.lastsendtime)
        }}</template>
      </el-table-column>
      <el-table-column label="发送状态" prop="issuccess" align="center">
        <template slot-scope="scope">
          <span slot="reference" v-if="scope.row.issuccess == '1'">
            成功{{ scope.row.countofphonenumber }}人</span
          >
          <span slot="reference" v-if="scope.row.issuccess == '0'"
            >失败{{ scope.row.countofphonenumber }}人</span
          >
          <a
            @click="getErro(scope.row)"
            v-if="scope.row.issuccess == '0'"
            style="color:blue"
            >查看原因</a
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="smscontent"
        label="短信内容"
        align="center"
        width="800"
      ></el-table-column>

      <el-table-column
        prop="type"
        label="发送人"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="logList.pageNum"
      :limit.sync="logList.pageSize"
      @pagination="getSuggestion()"
    />
    <!-- 查看全部弹接收人弹出框 -->
    <el-dialog
      :title="title"
      width="500px"
      :visible.sync="dialogVisible"
      style="margin-top: 10%"
    >
      <el-row type="flex" class="row-bg" justify="center">
        <el-col style="margin-left:20px">
          <div class="grid-content bg-purple">
            {{ contuneAll }}
          </div>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <!-- 查看失败状态弹出框 -->
    <el-dialog
      :title="title"
      width="500px"
      :visible.sync="errStatus"
      style="margin-top: 10%"
    >
      <el-row type="flex" class="row-bg" justify="center">
        <el-col style="margin-left:20px">
          <div class="grid-content bg-purple">
            {{ erros }}
          </div>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="errStatus = false">取 消</el-button>
        <el-button type="primary" @click="errStatus = false">确定</el-button>
      </span>
    </el-dialog>
    <!-- 发送短信弹出框 -->
    <el-dialog
      :title="title"
      width="650px"
      :visible.sync="sendText"
      style="margin-top: 10%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="19">
            <el-form-item label="接收人:" prop="iphones" label-width="115px">
              <el-input v-model="form.iphones" clearable placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="margin-left:15px">
            <el-button size="small" type="primary" @click="getRecipient"
              >添 加</el-button
            >
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="模板:" prop="formBoard" label-width="115px">
              <el-select
                v-model="formBoard"
                placeholder="请选择"
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in stencil"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="短信内容:"
              prop="refundDay"
              label-width="115px"
            >
              <el-col :span="24">
                <el-col :span="5">
                  您好!您购买的
                </el-col>
                <el-col :span="6">
                  <el-input
                    v-model="form.station"
                    placeholder="输入出发车站"
                    size="mini"
                  />
                </el-col>
                <el-col :span="1">
                  至
                </el-col>
                <el-col :span="5">
                  <el-input
                    v-model="form.target"
                    placeholder="输入目的地"
                    size="mini"
                  />
                </el-col>
                <el-col :span="3">
                  的班次,因
                </el-col>
                <el-col :span="4">
                  <el-input
                    v-model="form.reason"
                    placeholder="输入原因"
                    size="mini"
                  />
                </el-col>
              </el-col>
              <el-col :span="24">
                <el-col :span="3">
                  原因停班,
                </el-col>
                <el-col :span="12">
                  已为您在线办理全额退款，将在发车后
                </el-col>
                <el-col :span="5">
                  <el-input
                    v-model="form.refundDay"
                    placeholder="请输入时间"
                    size="mini"
                  />
                </el-col>
                <el-col :span="4">
                  个工作日原
                </el-col>
              </el-col>
              <el-col :span="24">
                <el-col :span="11">
                  路退回，如有疑问可以拨打客服电话
                </el-col>
                <el-col :span="5">
                  <el-input
                    v-model="form.phoneNumber"
                    size="mini"
                    placeholder="请输入"
                  />
                </el-col>
                <el-col :span="4">
                  进行咨询。
                </el-col>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendText = false">取 消</el-button>
        <el-button type="primary" @click="getSubmits">确定</el-button>
      </span>
    </el-dialog>
    <!-- 选择接收人弹出框 -->
    <el-dialog
      :title="title"
      width="1200px"
      :visible.sync="recipient"
      style="margin-top: 10%;margin:auto"
    >
      <el-form
        class="app-container"
        ref="elForm"
        :model="reception"
        size="medium"
        :inline="true"
        style="padding:0"
      >
        <el-form-item label="班次:" prop="bc" label-width="50px">
          <el-input
            v-model="reception.bc"
            placeholder="请输入"
            size="mini"
            clearable
            style="width:100px"
          ></el-input>
        </el-form-item>
        <el-form-item label="出发地:" prop="qczmc" label-width="60px">
          <el-input
            v-model="reception.qczmc"
            placeholder="请输入"
            size="mini"
            clearable
            style="width:100px"
          ></el-input>
        </el-form-item>
        <el-form-item label="目的地:" prop="target" label-width="60px">
          <el-input
            v-model="reception.target"
            placeholder="请输入"
            size="mini"
            clearable
            style="width:100px"
          ></el-input>
        </el-form-item>
        <el-form-item label="发车时间:" prop="rq" label-width="80px">
          <el-date-picker
            class="time"
            v-model="reception.rq"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="请选择"
            size="mini"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="姓名:" prop="username">
          <el-input
            v-model="reception.username"
            placeholder="请输入姓名"
            size="mini"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="userphone">
          <el-input
            v-model="reception.userphone"
            placeholder="请输入手机号"
            size="mini"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label prop="field107">
          <el-button
            type="primary"
            size="mini"
            v-hasPermi="['userManagement:userList:query']"
            @click="searchList"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="recipientList"
        border
        style="width: 95%; margin: auto"
        @selection-change="handleSelectionChange"
        :header-cell-style="{ background: '#f7f7f7' }"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column
          prop="username"
          label="用户名"
          align="center"
        ></el-table-column>
        <el-table-column
          label="姓名"
          prop="name"
          width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="iphoneNum"
          label="手机号"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="sourceType"
          label="用户类型"
          align="center"
        ></el-table-column>
        <el-table-column prop="registertime" label="注册时间" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.registTime != null">
              {{ newDate(scope.row.registTime) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="lastLoginTime"
          label=" 最后登录时间"
          width="155"
          align="center"
        >
          <!-- <template slot-scope="scope">{{
            newDate(scope.row.lastLoginTime)
          }}</template> -->
        </el-table-column>
      </el-table>
      <pagination
        v-show="totals > 0"
        :total="totals"
        :page.sync="reception.pageNum"
        :limit.sync="reception.pageSize"
        @pagination="searchList()"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="recipient = false">取 消</el-button>
        <el-button type="primary" @click="Submits">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  resetPwd,
  selectReceiver,
  sendSms
} from "@/api/marketing/noteList";
import utils from "../../js/index";
export default {
  name: "NoteList",
  components: {},
  props: [],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 用户列表
      tableData: [],
      // 接收人列表
      recipientList: [],
      // 查看全部弹出框
      dialogVisible: false,
      //发送短信弹出框
      sendText: false,
      //选择接收人弹出框
      recipient: false,
      // 数据总条数
      total: 0,
      // 接收人总条数
      totals: 0,
      //弹出框标题
      title: "",
      // 加载中
      loading: true,
      // 查看所有接收人
      contuneAll: "",
      // 查看失败原因
      errStatus: false,
      erros: "",
      // 模板
      formBoard: "停班",
      // 获取多选的人
      multipleChoice: "",
      stencil: [
        {
          label: "停班",
          value: 1
        }
      ],
      //发送状态
      field101Options: [
        {
          label: "发送成功",
          value: 1
        },
        {
          label: "发送失败",
          value: 0
        }
      ],

      // 查询接收人参数
      reception: {
        pageNum: 1,
        pageSize: 10,
        username: "",
        bc: "",
        qczmc: "",
        target: "",
        rq: "",
        userphone: ""
      },
      // 查询列表传递的参数
      logList: {
        pageNum: 1,
        pageSize: 10,
        lastsendtime: "",
        issuccess: "",
        smscontent: ""
      },
      // 表单参数
      form: {},
      // 表单验证
      rules: {
        iphones: [
          { required: true, message: "接收人不能为空", trigger: "blur" }
        ],
        refundDay: [
          { required: true, message: "请把内容填写完整", trigger: "blur" }
        ],
        radio: [{ required: true, message: "请选择发送时间", trigger: "blur" }]
      }
    };
  },
  computed: {},
  created() {
    this.getSuggestion();
  },
  mounted() {},
  methods: {
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      this.logList.page = val;
      this.currentPage = val;
    },
    // 用户列表
    getSuggestion() {
      list(this.logList).then(response => {
        if (response.code == 200) {
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
        } else {
          this.$message.error(response.msg);
        }
      });
    },
    //获取发送状态
    getStatus(val) {
      this.logList.issuccess = val;
    },
    // 搜索按钮
    search() {
      list(this.logList).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        iphones: undefined,
        station: undefined,
        target: undefined,
        startDateTime: undefined,
        reason: undefined,
        refundDay: undefined,
        phoneNumber: undefined
      };
    },
    // 查看全部接收人
    getAll(val) {
      this.title = "查看接收人";
      this.contuneAll = val.phonenumbers;
      this.dialogVisible = true;
    },
    // 查看失败原因
    getErro(val) {
      this.title = "错误原因";
      this.errStatus = true;
      this.erros = val.bak2;
    },
    // 发送短信按钮
    getSend() {
      this.reset();
      this.title = "发送短信";
      this.sendText = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      this.form.phoneNumber = "0371-96606";
      var _this = this;
      let yy = new Date().getFullYear();
      let mm =
        new Date().getMonth() + 1 < 10
          ? "0" + new Date().getMonth() + 1
          : new Date().getMonth() + 1;

      let dd =
        new Date().getDate() < 10
          ? "0" + new Date().getDate()
          : new Date().getDate();
      _this.form.startDateTime = yy + "-" + mm + "-" + dd;
    },
    // 发送短信
    getSubmits() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          sendSms(this.form).then(response => {
            if (response.code == 200) {
              this.sendText = false;
              this.getSuggestion();
              this.$message({
                message: "发送成功",
                type: "success"
              });
            } else {
              this.$message.error(response.msg);
            }
          });
        }
      });
    },
    //获取接收人
    getRecipient() {
      this.title = "添加接收人";
      this.recipient = true;
      this.recipientList = [];
      // this.reception = {};
    },
    // 搜索接收人
    searchList() {
      selectReceiver(this.reception).then(response => {
        if (response.code == 200) {
          this.recipientList = response.rows;
          this.totals = response.total;
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iphoneNum);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.ids = this.deWeight(this.ids);
    },
    Submits() {
      this.form.iphones = this.form.iphones + this.ids.join(",");
      this.recipient = false;
    },
    // 日期格式化
    newDate(date) {
      return utils.utils.NewDate(date);
    },
    //数组去重
    deWeight(arr) {
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i] == arr[j]) {
            arr.splice(j, 1);
            j--;
          }
        }
      }
      return arr;
    }
  }
};
</script>

<style>
.time {
  width: 123px !important;
}
.pagination-container {
  margin-top: 2%;
  margin-bottom: 2%;
}
.one-txt-cut {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
